package practica_CDLibrary;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.WindowConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JTextArea;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
import java.util.Vector;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class CD extends JFrame {

	private JPanel CDsPrestats;
	private JTextField jtfTitol;
	private JTextField jtfAutor;
	private JTextField jtfGenere;
	private JTextArea jtaPrestem;
	private BuscarReg dlgBuscar;
	Vector<CDBean> CDs;

	private JMenuBar jMenuBar;
	private JMenuItem jmItemAfegirReg;

	/**
	 * Launch the application.
	 */

	public Vector<CDBean> getCDs() {
		return CDs;
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CD frame = new CD();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * 
	 * @throws SQLException
	 */
	public CD()  {
		super();

		initGUI();
		CDs = new Vector<CDBean>();
		loadData();
		setResizable(false);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 537, 422);
		CDsPrestats = new JPanel();
		CDsPrestats.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(CDsPrestats);
		CDsPrestats.setLayout(null);

		JMenuBar menuBar = new JMenuBar();
		menuBar.setBounds(0, 0, 444, 21);
		CDsPrestats.add(menuBar);

		JMenu Arxiu = new JMenu("Arxiu");
		menuBar.add(Arxiu);

		JMenuItem jmItemAfegirReg = new JMenuItem("Afegir registre");
		jmItemAfegirReg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jmItemAfegirRegActionPerformed(e);
			}

		});

		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				sortirForm();
			}
		});

		Arxiu.add(jmItemAfegirReg);

		JMenuItem jmItemBuscarReg = new JMenuItem("Buscar registre");
		jmItemBuscarReg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				openBuscarReg();

			}
		});
		Arxiu.add(jmItemBuscarReg);

		JSeparator separator = new JSeparator();
		Arxiu.add(separator);

		JMenuItem jmItemSortir = new JMenuItem("Sortir");
		jmItemSortir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sortirForm();
			}
		});
		Arxiu.add(jmItemSortir);

		JLabel lblTitleLabel = new JLabel("Titol");
		lblTitleLabel.setBounds(20, 35, 50, 14);
		CDsPrestats.add(lblTitleLabel);

		JLabel lblAutorLabel = new JLabel("Autor");
		lblAutorLabel.setBounds(20, 75, 50, 14);
		CDsPrestats.add(lblAutorLabel);

		JLabel lblGenereLabel = new JLabel("Genere");
		lblGenereLabel.setBounds(20, 115, 50, 14);
		CDsPrestats.add(lblGenereLabel);

		JLabel lblPrestemLabel = new JLabel("Prestem");
		lblPrestemLabel.setBounds(20, 155, 100, 14);
		CDsPrestats.add(lblPrestemLabel);

		jtfTitol = new JTextField();
		jtfTitol.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				jtfFocusGained(e);
			}
		});
		jtfTitol.setBounds(100, 32, 275, 21);
		CDsPrestats.add(jtfTitol);
		jtfTitol.setColumns(10);

		jtfAutor = new JTextField();
		jtfAutor.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				jtfFocusGained(e);
			}
		});
		jtfAutor.setColumns(10);
		jtfAutor.setBounds(100, 72, 275, 20);
		CDsPrestats.add(jtfAutor);

		jtfGenere = new JTextField();
		jtfGenere.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				jtfFocusGained(e);
			}
		});
		jtfGenere.setColumns(10);
		jtfGenere.setBounds(100, 112, 275, 20);
		CDsPrestats.add(jtfGenere);

		jtaPrestem = new JTextArea();
		jtaPrestem.setLineWrap(true);
		jtaPrestem.setBounds(100, 150, 275, 70);
		CDsPrestats.add(jtaPrestem);

		JLabel jlbCreditos = new JLabel("@EMV 2015");
		jlbCreditos.setBounds(20, 247, 216, 14);
		CDsPrestats.add(jlbCreditos);

		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 10, 10);
		CDsPrestats.add(panel);

		JLabel jbTitol = new JLabel();

	}

	protected void openBuscarReg() {
		dlgBuscar = new BuscarReg(this);
		dlgBuscar.setVisible(true);

	}

	private void jmItemAfegirRegActionPerformed(ActionEvent e) {
		String titol = null, autor = null, genere = null, descripcio = null;

		titol = jtfTitol.getText().toString();

		// Comprobamos que el titulo no sea nulo
		if (titol.compareTo("") == 0) {
			JOptionPane.showMessageDialog(null,
					"El camp titol no pot estar buit");
			return;
		}
		autor = jtfAutor.getText().toString();
		genere = jtfGenere.getText().toString();
		descripcio = jtaPrestem.getText().toString();

		// CDs.add(new CDBean(titol, autor, genere, descripcio));
		Collections.sort(CDs);
		if (dlgBuscar != null && dlgBuscar.isVisible())
			dlgBuscar.actualitzarLlista();
	}

	private void initGUI() {
		try {
			this.setTitle("CDs prestados");
			this.setResizable(false);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			// getContentPane().setLayout(null);
			// pack();
			// setSize(132, 326);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void jtfFocusGained(FocusEvent evt) {

		javax.swing.text.JTextComponent jtb = null;

		jtb = (javax.swing.text.JTextComponent) evt.getSource();
		jtb.selectAll();
	}

	public void setRegDatos(int i) {
		CDBean cd = CDs.get(i);
		jtfTitol.setText(cd.getTitol());
		jtfAutor.setText(cd.getAutor());
		jtfGenere.setText(cd.getGenere());
		jtaPrestem.setText(cd.getPrestem());

	}

	private void sortirForm() {
		int respuesta = JOptionPane
				.showConfirmDialog(
						null,
						"Aquesta accio tancara la finestra. Esta segur que desitja continuar?",
						"Atencio", JOptionPane.YES_NO_OPTION);

		if (respuesta != JOptionPane.YES_OPTION) {
			return;
		}
		System.exit(0);
	}

	private void loadData(){

		try {
			ConnexioOk conn = new ConnexioOk();
			conn.openConnexio();

			Statement st = conn.getStatement();
			String sql1 = "select * from CD";
			ResultSet rs = conn.getStatement().executeQuery(sql1);

			while (rs.next()) {

				CDBean cd = new CDBean(rs.getInt("N_CD"),
						rs.getString("TITOL"), rs.getString("AUTOR"),
						rs.getString("GENERE"), rs.getString("PRESTEM"));
				System.out.println(cd.getTitol() + "--");
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
